(function(){

YAHOO.pendular.AutoComplete = function(config){
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        input: "pendular-search-input",
        container: "pendular-search-container",
        datasource: YAHOO.pendular.app.repositoryKeywordDataSource,
        options: {
            animVert: false
        }
    });
    YAHOO.pendular.AutoComplete.superclass.constructor.call(
        this,
        config.input,
        config.container, 
        config.datasource
    );
    this.itemSelectEvent.subscribe(this.itemSelected);
    this.keyListener = new YAHOO.util.KeyListener(
        config.input, {
            keys: [10,13]
        },
        function(){
            YAHOO.pendular.app.find();
        }            
    );
};

YAHOO.lang.extend(YAHOO.pendular.AutoComplete, YAHOO.widget.AutoComplete, {
    formatResult: function(data, query, match){
        var item = data[1],
            keyword = item.keyword,
            count = item.count;
        return  this.dataSource.prefix +
                keyword +
                " (" + count + ")"
        ;
    },
    itemSelected: function(type, a, b, c) {
        this.getInputEl().value = this.dataSource.prefix + a[2][1].keyword;
    },
    focus: function(){
        this.getInputEl().focus();
    },
    clear: function(){
        this.getInputEl().value = "";
    }
});
    
})();
